# This file generates Figures 1 and 5 in the main text
# Note the .do file "3. Main analyses.do" contains the code to create Figures 2-4

library(foreign)
library(ggplot2)
library(ggridges)
library(RColorBrewer)
 
all<-read.dta("/Users/jrthornton/Desktop/POBE 2021 replication/Data/ANES_irt_all.dta")
# Designates soph_cat as a factor variables
all$soph_cat<-as.factor(all$soph_cat)
all$n<-as.factor(all$n)
all$party<-as.factor(all$party)
 

############
# Figure 1 #
############

cols = c("maroon",  "orange", "lightblue") 
p <- ggplot(all, aes(x = equality_irt, y = as.numeric(soph_cat), fill = soph_cat)) +
	geom_density_ridges(alpha = 1/2,size=.25) +
	scale_x_continuous(expand = c(0.025,0.025),breaks=c(0,.25,.5,.75,1)) +
	scale_y_continuous(expand = c(0.025, .025) , breaks=c(1,2,3), labels=c("Low","Moderate","High"))
dev.new(width=6,height=3)
dev.size()	
p +	theme(legend.position = "none") + theme_linedraw() +
	scale_fill_manual(values = cols, guide = "none") +
	facet_wrap( ~ party, ncol=3) +
	xlab("Support for Equality") +
	ylab("Density by Sophistication") +
	theme(aspect.ratio=1.0) + 
# Changes defaults of theme_linedraw()
	theme(panel.grid.minor = element_blank()) +
	theme(panel.grid.major = element_line(colour = "black",size=0.5, linetype="dotted")) + 
	theme(axis.text.y = element_text(angle = 0 , color="black" , hjust=)) +
	theme(axis.text.x = element_text(color="black")) +
	theme(panel.grid.major.x = element_blank()) +
	theme(panel.border = element_rect(fill=NA, colour = "black", size=.75)) +
		theme(strip.background = element_rect(color="white",fill="white",size=1)) +  theme(strip.text = element_text(color = 'black'))
 + theme(strip.text=element_text(vjust=-1))
 
############
# Figure 5 #
############
library(ggplot2)
soph_coefs <-read.csv("/Users/jrthornton/Desktop/POBE 2021 replication/Data/fig5.csv")


dev.new(width=4,height=3)
dev.size()
ggplot(soph_coefs, aes(x=variable, y= ceof)) + 
	geom_linerange(mapping=aes(x=variable,ymin=lower,ymax=upper),size=.33) +
	geom_point(aes(y= ceof),size=1.667,shape=21,fill="white") + 
	coord_flip() + 
	theme_linedraw() + 
	ylab("Coefficient for\nSophistication") + 
	xlab(" ")	+ 
	theme(panel.grid.major = element_blank()) + 	
	theme(panel.grid.minor = element_blank()) + 
	geom_hline(yintercept=0, linetype="solid", color = "grey75", size=.5) + 
	scale_y_continuous(expand = c(0.005, 0.005), breaks=c(-.05, 0 , .05, .1 , .15))
